package com.neoCalf.neoUser;

import com.neoCalf.neoCommon.redis.RedisTool;
import com.neoCalf.neoUser.service.SysSettingService;
import com.neoCalf.neoUser.service.UserInfoBeautyService;
import com.neoCalf.neoUser.service.UserInfoService;
import com.neoCalf.neoUser.webSocket.NettyWebSocketStarter;
import com.neoCalf.neoUser.webSocket.handler.HandlerMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;


@Component("initRun")
@Slf4j
public class InitRun implements ApplicationRunner {
    @Resource
    private DataSource dataSource;
    @Resource
    private RedisTool<Object> redisTool;
    @Resource
    private NettyWebSocketStarter nettyWebSocketStarter;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private SysSettingService sysSettingService;
    @Resource
    private UserInfoBeautyService userInfoBeautyService;
    @Resource
    private HandlerMessage handlerMessage;

    @Override
    public void run(ApplicationArguments args) {
        try {
            dataSource.getConnection();
            redisTool.get("test");
            initData();
            new Thread(nettyWebSocketStarter).start();
            handlerMessage.ListenerMessage();
            handlerMessage.addChannelByUserIdToGroup();
            log.info("================================服务器启动成功================================");
        } catch (SQLException | RedisConnectionFailureException e) {
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("服务器启动失败", e);
        }
    }

    public void initData() {
        sysSettingService.initSysSetting(); // 初始化系统设置
        userInfoBeautyService.initUserInfoBeauty(); // 初始化靓号
        userInfoService.initRobotAccount(); // 初始化机器人账号
    }
}
